package cn.sxt.yxl.question.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.dao.IAColumnsDao;
import cn.sxt.yxl.question.dao.IAItemsDao;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.service.IQuestionService;

@Service("questionService")
public class QuestionServiceImpl implements IQuestionService{
	@Resource
	private IAColumnsDao columnsDao;
	@Resource
	private IAItemsDao itemsDao;
	
	/**
	 * 查询单个栏目
	 */
	@Override
	public AColumns findOneColumnsService(Map<String, Object> condMap) {
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			condMap.put("id", Integer.valueOf(condMap.get("id") + ""));
		}
		return columnsDao.findOne(condMap);
	}
	/**
	 * 添加单个栏目
	 */
	@Override
	public JSONObject saveOneColumnsService(AColumns columns) {
		JSONObject jsonObject = new JSONObject();
		int res=columnsDao.saveOne(columns);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", columns.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
		
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public JSONObject updateOneColumnsService(AColumns columns) {
		JSONObject jsonObject = new JSONObject();
		int res=columnsDao.updateOne(columns);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", columns.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
		
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject deleteOneColumnsService(AColumns columns) {
		JSONObject jsonObject = new JSONObject();
		int res=columnsDao.deleteOne(columns);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", columns.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
		
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public List<AColumns> findCondListColumnsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		
		if(pageInfoUtil!=null){
			//分页
			Page<?> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			//查询一遍
			List<AColumns> columnsList = this.columnsDao.findCondList(condMap);
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return columnsList;
			
		}
		//不分页
		return columnsDao.findCondList(condMap);
	}
	/**
	 * 查询单个测试
	 */
	@Override
	public AItems findOneItemsService(Map<String, Object> condMap) {
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			condMap.put("id", Integer.valueOf(condMap.get("id") + ""));
		}
		return itemsDao.findOne(condMap);
	}
	/**
	 * 添加单个测试
	 */
	@Override
	public JSONObject saveOneItemsService(AItems items) {
		JSONObject jsonObject = new JSONObject();
		int res=itemsDao.saveOne(items);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", items.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
			
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public JSONObject updateOneItemsService(AItems items) {
		JSONObject jsonObject = new JSONObject();
		int res=itemsDao.updateOne(items);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", items.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
			
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject deleteOneItemsService(AItems items) {
		JSONObject jsonObject = new JSONObject();
		int res=itemsDao.deleteOne(items);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", items.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
			
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public List<AItems> findCondListItemsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		
		if(pageInfoUtil!=null){
			//分页
			Page<?> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			//查询一遍
			List<AItems> itemsList = this.itemsDao.findCondList(condMap);
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return itemsList;
			
		}
		//不分页
		return itemsDao.findCondList(condMap);
	}
	
}
